package com.ruoyi.web.controller.consumer;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumer.domain.PaymentRecord;
import com.ruoyi.consumer.domain.query.PaymentRecordQuery;
import com.ruoyi.consumer.domain.query.WechatPayQuery;
import com.ruoyi.consumer.service.IPaymentRecordService;
import com.ruoyi.consumer.service.IWechatPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 微信支付
 * @author lisongtao
 * @date 2023/11/8 22:54
 */
@RestController
@RequestMapping("/weChat")
public class WechatPayController extends BaseController {


    @Autowired
    private IWechatPayService wechatPayService;

    @Autowired
    private IPaymentRecordService paymentRecordService;


    @Log(title = "微信支付预支付操作")
    @PostMapping("/unifiedOrder")
    public AjaxResult unifiedOrder(@RequestBody @Validated WechatPayQuery payQuery) throws WxPayException {
        return AjaxResult.success(wechatPayService.unifiedOrder(payQuery));
    }


    @Anonymous
    @PostMapping("/notify")
    public AjaxResult weChatPayNotify(@RequestBody String xmlData) throws WxPayException {
        wechatPayService.weChatPayNotify(xmlData);
        return AjaxResult.success();
    }

    /**
     * 循环记录
     */
    @PostMapping("/pollPaymentRecords")
    public AjaxResult pollPaymentRecords() {
        wechatPayService.pollPaymentRecords();
        return AjaxResult.success();
    }


    /**
     * 查询支付中订单
     */
    @Log(title = "查询支付中订单")
    @PostMapping("/queryUnderPayOrder")
    public AjaxResult queryOrder(@RequestBody PaymentRecordQuery paymentRecord) {
        try {
            wechatPayService.queryUnderPayOrder(paymentRecord.getPlanId());
        } catch (WxPayException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getErrCodeDes());
        }
        return AjaxResult.success();
    }


    /**
     * 取消支付订单记录
     */
    @Log(title = "取消支付订单记录")
    @PostMapping("/closeOrder")
    public AjaxResult closeOrder(@RequestBody PaymentRecordQuery paymentRecord) {
        try {
            wechatPayService.closeOrder(paymentRecord.getOutTradeNo(), "取消");
        } catch (WxPayException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getErrCodeDes());
        }
        return AjaxResult.success();
    }

    /**
     * 查询微信支付记录列表
     */
    @PreAuthorize("@ss.hasRole('financeAdmin')")
    @GetMapping("/list")
    public TableDataInfo list(PaymentRecord paymentRecord) {
        startPage();
        List<PaymentRecord> list = paymentRecordService.selectPaymentRecordList(paymentRecord);
        return getDataTable(list);
    }

    /**
     * 导出微信支付记录列表
     */
    @PreAuthorize("@ss.hasPermi('financeAdmin')")
    @Log(title = "微信支付记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaymentRecord paymentRecord) {
        List<PaymentRecord> list = paymentRecordService.selectPaymentRecordList(paymentRecord);
        ExcelUtil<PaymentRecord> util = new ExcelUtil<>(PaymentRecord.class);
        util.exportExcel(response, list, "微信支付记录数据");
    }

    /**
     * 获取微信支付记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('financeAdmin')")
    @PostMapping(value = "/getPaymentRecordInfo")
    public AjaxResult getInfo(@RequestBody PaymentRecord paymentRecord) {
        return success(paymentRecordService.selectPaymentRecordById(paymentRecord.getId()));
    }

    /**
     * 根据id删除支付记录
     */
    @Log(title = "删除支付记录", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('financeAdmin')")
    @PostMapping(value = "/deleteById")
    public AjaxResult deletePaymentRecordById(@RequestBody PaymentRecord paymentRecord) {
        paymentRecordService.deletePaymentRecordById(paymentRecord.getId());
        return AjaxResult.success();
    }



}
